package com.twu.biblioteca;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;

public class BookList {
    private ArrayList<Book> books = new ArrayList<Book>();

    public BookList() {
    }

    public BookList(String xmlFilePath) {
        parse(xmlFilePath);
    }

    private void parse(String xmlFilePath) {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        Document document = null;
        Element rootElement = null;

        try {
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            document = builder.parse(new File(xmlFilePath));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        rootElement = document.getDocumentElement();
        NodeList bookList = rootElement.getElementsByTagName("Book");

        if (bookList != null) {
            for (int i = 0; i < bookList.getLength(); i++) {
                Node bookNode = bookList.item(i);
                NodeList infoNodeList = bookNode.getChildNodes();
                int number = Integer.parseInt(infoNodeList.item(1).getTextContent());
                String name = infoNodeList.item(3).getTextContent();
                String author = infoNodeList.item(5).getTextContent();
                int publishedYear = Integer.parseInt(infoNodeList.item(7).getTextContent());

                this.books.add(new Book(number, name, author, publishedYear));
            }
        }
    }

    public int getBooksCount() {
        return this.books.size();
    }

    public Book getBookByNumber(int number) {
        for (int i = 0; i < this.books.size(); i++) {
            Book book = this.books.get(i);
            if (book.getNumber() == number) {
                return book;
            }
        }
        return null;
    }

    public Book removeBookByNumber(int number) {
        Book foundBook = null;

        for (int i = 0; i < getBooksCount(); i++) {
            if (this.books.get(i).getNumber() == number) {
                foundBook = this.books.get(i);
                break;
            }
        }
        if (foundBook != null) {
            this.books.remove(foundBook);
        }
        return foundBook;
    }

    public void add(Book book) {
        this.books.add(book);
    }

    public void printBookList() {
        printBookListHead();
        printAllBooksInformation();
        printBookListEnd();
    }

    public void printBookListHead() {
        String bookListHead = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" +
                "                                     The Book List\n" +
                "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
        System.out.println(bookListHead);
    }

    public void printAllBooksInformation() {
        int size = this.books.size();

        if (size == 0) {
            System.out.println("  The book list is empty\n");
        } else {
            System.out.println(String.format("%s    %-30s    %-20s    %s\n", "Number", "Name", "Author", "PublishedYear"));
            for (int i = 0; i < size; i++) {
                this.books.get(i).printInformation();
            }
        }
    }

    public void printBookListEnd() {
        System.out.println("++++++++++++++++++++++++++++++++++++++  End  +++++++++++++++++++++++++++++++++++++++\n");
    }

    public void printCheckoutBooksInformation() {
        int size = this.books.size();

        if (size == 0) {
            System.out.println("  The checkout book list is empty\n");
        } else {
            System.out.println(String.format("%s    %-30s    %-20s    %s    %s\n", "Number", "Name", "Author", "PublishedYear", "Library Number"));
            for (int i = 0; i < size; i++) {
                this.books.get(i).printInformation();
            }
        }
    }
}
